// pages/history/history.js
const allUrl = require('../../config').allUrl
var app = getApp()
var openId = null

Page({
  data:{
    currentPage: 1,
    pageCount: 10,
    waitInfo:[],
      // {address:"北京市东城区正义路2号正义路"
          // created_at:"2017-05-05 14:23:28"
          // done_at:"0000-00-00 00:00:00"
          // call_id:"11"
          // j_id:"0"
          // lat:"39.90403"
          // lng:"116.407526"
          // mobile:"15637196121"
          // openid:"o1oL70HabL7LOqdJzrmMkxTkvmFw"
          // remark:"这是一条备注"
          // status:"0"}
      isRequesting: true,  //是否正在请求数据
      isAllData: false, //是否已经请求完所有数据
  },


  onLoad:function(options){
    var that = this
    openId = app.globalData.openId
    
    // 页面初始化 options为页面跳转所带来的参数
    // this.onPullDownRefresh()

    wx.showToast({
      title: '正在加载...',
      icon: 'loading'
    })
    this.onceReq(function(res){
      wx.stopPullDownRefresh()
      that.setData({
        isRequesting: false
      })
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },





  //  查看详情
  lookDetail: function(e){
    var that = this

    wx.navigateTo({
      url: '../alert/alert?call_id='+that.data.waitInfo[e.target.dataset.index].call_id,
      success: function(res){},
      fail: function(res) {},
      complete: function(res) {}
    })
  },


  //下拉刷新
  onPullDownRefresh: function(){
    console.log('下拉刷新')
    this.setData({
      currentPage: 1
    })
    wx.showToast({
      title: '正在刷新...',
      icon: 'loading',
      duration: 5000
    })
    this.onceReq(function(res){
      wx.stopPullDownRefresh()
      wx.hideToast()
    })
  },
  // 上拉加载
  onReachBottom: function(){
    console.log('circle 下一页')
    var that = this
    if(this.data.isRequesting==false){//如果没有进行请求
      if(this.data.isAllData){
        if(this.data.waitInfo.length>4){
          wx.showToast({
            title: "没有更多数据"
          })
        }
        return;
      }
      this.setData({
        currentPage: this.data.currentPage+1,
        isRequesting: true
      })
      wx.showToast({
        title: '加载更多...',
        icon: 'loading',
        duration: 5000
      })
      this.onceReq(function(res){
        that.setData({
          isRequesting: false
        })
        wx.stopPullDownRefresh()
        wx.hideToast()
      })
    }
  },
  //  一次请求记录列表
  onceReq: function(complete){
    var that = this
    // console.log('当前加载页……'+this.data.currentPage)
    wx.request({
      url: allUrl + 'Api/getListByOpenid.html',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openId,
        currentPage: that.data.currentPage,
        pageCount: that.data.pageCount
      },
      method: 'POST', 
      success: function(res){
        // console.log('history page: list..')
        // console.log(res)
        console.log(res.data)
        // if(res.data.data.length!=10){
        //   that.setData({
        //     isAllData: true
        //   })
        // }
        
        if(res.data.data==null || res.data.data.length<0) {
          if(that.data.currentPage>1){
            that.setData({
              currentPage: that.data.currentPage-1
            })
          }
          return;
        }
        if(that.data.currentPage == 1){//当是在首页的时候，直接替换数据源
          that.setData({
            waitInfo: res.data.data
          })
        }else{  //  向以前的数组中添加数据
          var p = that.data.waitInfo.concat(res.data.data)
          that.setData({
            waitInfo: p
          })
        }
        //如果已经完全返回完，那么就记录
        if(that.data.waitInfo.length==res.data.count){
          that.setData({
            isAllData: true
          })
        }
      },
      fail: function(res) {
        if(that.data.currentPage>1){
            that.setData({
              currentPage: that.data.currentPage-1
            })
        }
      },
      complete: function(res) {complete(res)}
    })
  }

})